﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Markup;
using Microsoft.Phone.Tasks;
using System.Globalization;
using Microsoft.Advertising.Mobile.UI;
using System.Xml.Linq;
using System.Windows.Navigation;
using System.ComponentModel;
using Common.Library;
using OnlineVideos.Library;
using OnlineVideos.UI;
using OnlineVideos.Data;
using OnlineVideos.Common;
using OnlineVideos.View_Models;
using OnlineVideos.Entities;
using System.Threading;
using OnlineVideos.Utilities;
using Common.Utilities;

namespace OnlineVideos
{
    public partial class Favorites : PhoneApplicationPage
    {
        #region Global
        private SolidColorBrush adcontrolborder = new SolidColorBrush(Colors.Transparent);
        List<ShowLinks> objAudioList;

        //List<SubjectsTable> objSubjectlist = new List<SubjectsTable>();
        string link = string.Empty;
        List<ShowLinks> showFavVideos = null;
        List<ShowLinks> showFavaudiosongs = null;
        List<ShowList> showFavList = null;
      
        //ListBox lbxVidoes;
      

        #endregion

        public Favorites()
        {
            InitializeComponent();
            objAudioList = new List<ShowLinks>();
            showFavVideos = new List<ShowLinks>();
            showFavList = new List<ShowList>();
        }
        private void LoadAds()
        {
            try
            {
                PageHelper.LoadAdControl(LayoutRoot, adstackpl, 1);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadAds Method In SongDetails file", ex);
                string excepmess = "Exception in LoadAds Method In SongDetails file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
            }

        }  
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                LoadAds();
                FlurryWP8SDK.Api.LogEvent("Favorites Page", true);
                LoadPivotThemes();               
                GetPageDataInBackground();
                if (pvtMainFavorites.Items.Count >= 2)
                 pvtMainFavorites.SelectedIndex =0;
              
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In Favorites.cs file.", ex);
            }
        }
        private void GetPageDataInBackground()
        {
            BackgroundHelper bwHelper = new BackgroundHelper();
            if (AppResources.ShowDetailPageAudioPivot)
            {
                bwHelper.AddBackgroundTask(
                                           (object s, DoWorkEventArgs a) =>
                                           {
                                               a.Result = FavoritesManager.GetFavoriteAudioLinks();
                                           },
                                           (object s, RunWorkerCompletedEventArgs a) =>
                                           {
                                               showFavaudiosongs = (List<ShowLinks>)a.Result;
                                               if (showFavaudiosongs.Count > 0)
                                               {
                                                   lbxFavoriteaudio.ItemsSource = showFavaudiosongs;
                                                   tblkFavNoAudio.Visibility = Visibility.Collapsed;
                                               }
                                               else
                                               {
                                                   tblkFavNoAudio.Visibility = Visibility.Visible;
                                                   tblkFavNoAudio.Text = "No " + AppResources.ShowFavoriteAudioPivotTitle + " available";
                                               }

                                           }
                                         );
            }
            else
            {
                bwHelper.AddBackgroundTask(
                                            (object s, DoWorkEventArgs a) =>
                                            {
                                                a.Result = FavoritesManager.GetFavoriteLinks();
                                            },
                                            (object s, RunWorkerCompletedEventArgs a) =>
                                            {


                                                showFavVideos = (List<ShowLinks>)a.Result;
                                                if (showFavVideos.Count > 0)
                                                {
                                                    lbxFavoritesongs.ItemsSource = showFavVideos;

                                                    tblkFavNoSongs.Visibility = Visibility.Collapsed;
                                                }
                                                else
                                                {
                                                    tblkFavNoSongs.Text = "No " + AppResources.FavoriteSongsPivotTitle + " available";
                                                    tblkFavNoSongs.Visibility = Visibility.Visible;
                                                }
                                            }
                                          );




                if (AppResources.ShowFavoritesPageMoviesPivot)
                {
                    bwHelper.AddBackgroundTask(
                                                (object s, DoWorkEventArgs a) =>
                                                {
                                                    a.Result = FavoritesManager.GetFavoriteShows();
                                                },
                                                (object s, RunWorkerCompletedEventArgs a) =>
                                                {
                                                    showFavList = (List<ShowList>)a.Result;
                                                    if (showFavList.Count > 0)
                                                    {
                                                        lbxVidoes.ItemsSource = showFavList;
                                                        tblk.Visibility = Visibility.Collapsed;
                                                    }
                                                    else
                                                    {
                                                        tblk.Visibility = Visibility.Visible;
                                                        tblk.Text = "No " + AppResources.FavoriteMoviesPivotTitle + " available";
                                                    }

                                                }
                                              );
                }





            }
            bwHelper.RunBackgroundWorkers();
        }
             
        void lbxMovies_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxVidoes.SelectedIndex == -1)
                    return;

                //TODO: verify this logic
                if (!AppResources.ShowFavoritesPageMoviesPivot)
                {
                    List<ShowLinks> showLinks =(OnlineShow.GetShowLinksByType((lbxVidoes.SelectedItem as ShowLinks).ShowID.ToString(), LinkType.Movies)).ToList();
                    foreach (ShowLinks objLinks in showLinks)
                    {
                        link = objLinks.LinkUrl;
                    }
                    YoutubeApi.Youtube.PlayYoutubeVideo(link);
                }
                else
                {
                    if (!AppResources.IsSimpleDetailPage)
                        NavigationService.Navigate(NavigationHelper.getMovieDetailPage(lbxVidoes.SelectedItem as INavigationContext));
                    else
                        NavigationService.Navigate(NavigationHelper.getMovieDetailPage(lbxVidoes.SelectedItem as INavigationContext));
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxMovies_SelectionChanged Method In Favorites.cs file.", ex);
            }
            lbxVidoes.SelectedIndex =-1;
        }

        private void LoadPivotThemes()
        {
            try
            {
                
                if (AppResources.ShowDetailPageAudioVideoHeaders)
                {
                    pvtitemSongs.Visibility = Visibility.Visible;
                    tblkFavNoSongs.Visibility = Visibility.Visible;
                    pvtitemSongs.Header = AppResources.FavoriteSongsPivotTitle;
                }
                else
                {
                    pvtitemSongs.Visibility = Visibility.Collapsed;
                    pvtMainFavorites.Items.Remove(pvtitemSongs);

                  
                }
                if (AppResources.ShowDetailPageAudioPivot)
                {
                    pvtitemAudio.Visibility = Visibility.Visible;
                    tblkFavNoAudio.Visibility = Visibility.Visible;
                    pvtitemAudio.Header = AppResources.ShowFavoriteAudioPivotTitle;
                   
                }
                else
                {
                    pvtitemAudio.Visibility = Visibility.Collapsed;
                    pvtMainFavorites.Items.Remove(pvtitemAudio);
                }
                if (!AppResources.ShowFavoritesPageMoviesPivot)
                {
                    pvtitmVideos.Visibility = Visibility.Collapsed;
                    pvtMainFavorites.Items.Remove(pvtitmVideos);
                }
                else
                {
                    pvtitmVideos.Visibility = Visibility.Visible;
                    tblk.Visibility = Visibility.Visible;
                    pvtitmVideos.Header = AppResources.FavoriteMoviesPivotTitle;

                }
               
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadPivotThemes Method In Favorites.cs file.", ex);
            }
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("Favorites Page");
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In Favorites.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In Favorites.cs file.", ex);
            }
        }

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
           
            base.OnBackKeyPress(e);
        }

        private void lbxFavoritesongs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxFavoritesongs.SelectedIndex == -1)
                    return;
                string Url = (lbxFavoritesongs.SelectedItem as ShowLinks).LinkUrl;
                YoutubeApi.Youtube.PlayYoutubeVideo(Url);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxFavoritesongs_SelectionChanged Method In Favorites.cs file.", ex);
            }
            lbxFavoritesongs.SelectedIndex = -1;
        }

        private void btnFavSong_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OnlineVideos.View_Models.ShowDetail detailModel = new OnlineVideos.View_Models.ShowDetail();
                detailModel.AddToFavorites(lbxFavoritesongs, sender as MenuItem, LinkType.Songs);
                NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in btnFavSong_Click Method In Favorites.cs file.", ex);
            }
        }

        private void imgTitle_MouseEnter(object sender, MouseEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);


        }
        private void lbxFavoriteaudio_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbxFavoriteaudio.SelectedIndex == -1)
                return;
            //ShowAudio s = new ShowAudio();

            string id = (lbxFavoriteaudio.SelectedItem as ShowLinks).LinkID.ToString();
            string u = (lbxFavoriteaudio.SelectedItem as ShowLinks).Songplay;
            string title = (lbxFavoriteaudio.SelectedItem as ShowLinks).Title;
            string link = (lbxFavoriteaudio.SelectedItem as ShowLinks).LinkUrl;
           //s.LoadDownLoads(id, u, title, link);
            AppSettings.SongID = (lbxFavoriteaudio.SelectedItem as ShowLinks).LinkID.ToString();
            AppSettings.AudioImage = (lbxFavoriteaudio.SelectedItem as ShowLinks).Songplay;
            Thread AudioThread = new Thread(() =>
            {
                objAudioList = FavoritesManager.GetFavoriteAudioLinks();
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    lbxFavoriteaudio.ItemsSource = objAudioList;
                });

            });
            AudioThread.Start();
            lbxFavoriteaudio.SelectedIndex = -1;
        }

        private void btnAudioFavSong_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OnlineVideos.View_Models.ShowDetail detailModel = new OnlineVideos.View_Models.ShowDetail();
                detailModel.AddToFavorites(lbxFavoriteaudio, sender as MenuItem, LinkType.Audio);
                NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in btnAudioFavSong_Click Method In Favorites.cs file.", ex);
            }
        }
        
         
    }
}